package cn.fl.custpay.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.custpay.condition.CmsPayFreeQueryCondition;
import cn.fl.custpay.service.ICmmRechargeRecordService;
import cn.fl.custpay.service.ICmsPayFreeService;
import cn.fl.custpay.vo.CmsPayFreeVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;

/**
 * @Description: 客户免手续费活动表 控制层
 */
@Controller
@RequestMapping("/custpay/cmsPayFree")
public class CmsPayFreeController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmsPayFreeController.class);

	/**
	 * CmsPayFree 服务层BEAN
	 */
	@Autowired
	private ICmsPayFreeService cmsPayFreeService;

	@Autowired
	private ICmmRechargeRecordService cmmRechargeRecordService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toCmsPayFreeList.do", method = { RequestMethod.GET})
	public ModelAndView toCmsPayFreeList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("custpay/cmsPayFree/cmsPayFreeList");
		List<KeyValueVO> orglist = this.cmmRechargeRecordService.searchListByOrgTypCd(CurrentThreadContext.getFlServiceSysCd());
		view.addObject("orglist", orglist);
		BaseLogUtils.info(logger, "toCmsPayFreeList", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("custpay/cmsPayFree/modify");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initCmsPayFreeList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initCmsPayFreeList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String isdept = request.getParameter("isdept");
		String phone = request.getParameter("phone");
		String cstNm = request.getParameter("cstNm");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		CmsPayFreeQueryCondition qc = new CmsPayFreeQueryCondition();
		if (StringUtils.isNotEmpty(isdept)) {
			qc.setIsdept(isdept);
		}
		if (StringUtils.isNotEmpty(phone)) {
			qc.setPhone(phone);
		}
		if (StringUtils.isNotEmpty(cstNm)) {
			qc.setCstNm(cstNm);
		}
		page.setCondition(qc);
		
		//TODO SOMETHING
		Pager pager = this.cmsPayFreeService.searchListPage(page);
		List<CmsPayFreeVO> list = (List<CmsPayFreeVO>)pager.getResultList();
		for(CmsPayFreeVO _vo : list){
			if(StringUtils.isNotEmpty(_vo.getIsdept())){
				_vo.setIsdept(this.cmmRechargeRecordService.searchOrgNm(_vo.getIsdept(), CurrentThreadContext.getFlServiceSysCd()));
			}
		}
		ListPageVO<CmsPayFreeVO> listPage = new ListPageVO<CmsPayFreeVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initCmsPayFreeList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.cmsPayFreeService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			CmsPayFreeVO vo = this.cmsPayFreeService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/addCmsPayFree.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addCmsPayFree(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String addphone = request.getParameter("addphone");
		String addfreeNum = request.getParameter("addfreeNum");
		String addisdept = request.getParameter("addisdept");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		if(StringUtils.isEmpty(addphone) || StringUtils.isEmpty(addfreeNum) || StringUtils.isEmpty(addisdept)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_PARAM_IS_BAD);
			BaseLogUtils.error(logger, "addCmsPayFree", "新增数据,参数不完整.", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		CmsPayFreeVO entity = null;
		try {
			CstBscInfoVO cstvo = null;
			entity = new CmsPayFreeVO();
			if(StringUtils.isNotEmpty(addphone)){
				cstvo = this.cmsPayFreeService.searchCstIdNm(addphone);
			}
			if(null != cstvo && null != cstvo.getId()){
				entity.setEmployeeId(String.valueOf(cstvo.getId()));
				if(StringUtils.isNotEmpty(addfreeNum)){
					entity.setFreeNum(addfreeNum);
				}
				if(StringUtils.isNotEmpty(addisdept)){
					entity.setIsdept(addisdept);
				}
				entity.setCrtTm(new Date());
				entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				Long id = this.cmsPayFreeService.add(entity);
				if(null == id){
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo(SysLogConstant.LOG_CMM_ADD_FAIL);
				}
				BaseLogUtils.info(logger, "addCmsPayFree", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}else {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("该手机号不是系统客户！");
			}

		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "addCmsPayFree", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addCmsPayFree", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modifyCmsPayFree.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modifyCmsPayFree(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String modifyfreeNum = request.getParameter("modifyfreeNum");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modifyCmsPayFree", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		CmsPayFreeVO entity = null;
		try {
			entity = new CmsPayFreeVO();
			entity.setId(Long.valueOf(id));
			entity.setMdfTm(new Date());
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			if(StringUtils.isNotEmpty(modifyfreeNum)){
				entity.setFreeNum(modifyfreeNum);
			}
			boolean success = this.cmsPayFreeService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modifyCmsPayFree", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modifyCmsPayFree", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modifyCmsPayFree", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modifyCmsPayFree", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/removeCmsPayFree.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object removeCmsPayFree(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "removeCmsPayFree", "删除数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		CmsPayFreeVO entity = null;
		try {
			entity = new CmsPayFreeVO();
			entity.setId(Long.valueOf(id));
			entity.setIsDel(1);
			entity.setMdfTm(new Date());
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			boolean success = this.cmsPayFreeService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "removeCmsPayFree", "删除失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "removeCmsPayFree", "删除成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeCmsPayFree", "删除失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeCmsPayFree", "删除失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
}

